﻿/// <reference path="_namespace.js" />
/// <reference path="../components/MenuItemDisplayFlags.js" />


/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.windows.Console
/*****************************************************************************/

ajax.lang.addNamespace("ajax.windows");
ajax.lang.imports("ajax.components.MenuItemDisplayFlags");

//-----------------------------------------------------------------------------
// Constructor
ajax.windows.Console = function(doc, sLayerName, w, h, displayFlags, sText, parentNode, bModal, taskBar)
{   
    var self            = this;
    
       
    ajax.windows.Console.superConstructor.call(this, doc, sLayerName, w, h, displayFlags, sText, parentNode, bModal, taskBar);     
    this.log = "";  
    
    // Define styles
    this.itemStyle = 
    {
        // Set style to look like a DOS window  
        fontFamily           : "Courier New, Courier, mono",
        fontSize             : "12px",
        backgroundColor      : "#000000",
        color                : "#00FF00",
        overflow             : "auto"
    }; 
    
    
    this.getEvent(this.window, "onLoad").addListener(this.onLoadHandler);                
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.windows.Console", "ajax.windows.WinForm");
if ($getDesignMode())
    ajax.windows.Console.prototype = ajax.windows.WinForm.prototype;

//-----------------------------------------------------------------------------
ajax.windows.Console.prototype.createMenu = function()
{
    // Add Menu
    var itemFlags   = ajax.components.MenuItemDisplayFlags;    

    // create file menu
    this.fileMenu           = ajax.lang.safeNew("ajax.components.Menu", this.getDocument(), "", 200, 30, this.getLayer());
    this.fileMenu           = ajax.components.Menu.cast(this.fileMenu);    
    this.fileMenu.animated  = true;
    //Item1
    var fileMenuExit    = ajax.lang.safeNew("ajax.components.MenuItem", this.getDocument(), "Exit", 
        ajax.getDomainPath() + "/images/red_cross.gif", "", null, itemFlags.Text | itemFlags.Image);
    fileMenuExit = ajax.components.MenuItem.cast(fileMenuExit);
    
    this.fileMenu.addMenuItem(fileMenuExit);
    var hideFileMenu = this.fileMenu.hide.bind(this.fileMenu);
    
    fileMenuExit.getEvent(this.getWindow(), "onSelected").addListener(function(e)
        {
            hideFileMenu();
            this.close();
        }.bind(this)
    );       
    ajax.lang.getEvent(this.getWindow(), this.getDocument(), "onmouseup").addListener(hideFileMenu);
                
                
    // Create Options menu
    this.optionsMenu        = ajax.lang.safeNew("ajax.components.Menu", this.getDocument(), "", 200, 70, this.getLayer());
    this.optionsMenu        = ajax.components.Menu.cast(this.optionsMenu);
    this.optionsMenu.animated    = true;
    
    // Item1
    var optionsMenuDebugger = ajax.lang.safeNew("ajax.components.MenuItem", this.getDocument(), "Debug Output Target",
        "", "", null, itemFlags.Text | itemFlags.Checkbox, false);
    optionsMenuDebugger     = ajax.components.MenuItem.cast(optionsMenuDebugger);
    
    this.optionsMenu.addMenuItem(optionsMenuDebugger);
    var hideOptionsMenu = this.optionsMenu.hide.bind(this.optionsMenu);
    
    optionsMenuDebugger.getEvent(this.getWindow(), "onSelected").addListener(function(e)
        {
            hideOptionsMenu();
            if (optionsMenuDebugger.checked)
            {
                ajax.debug.Output = this;
                ajax.debug.Output.writeLn("debug Output set to this window");
            }
            else
            {
                ajax.debug.Output = ajax.debug.defaultOutput;
                ajax.debug.Output.writeLn("debug Output set back to default buffer");
            }
        }.bind(this)
    );
    ajax.lang.getEvent(this.getWindow(), this.getDocument(), "onmouseup").addListener(hideOptionsMenu);
    
    // Spacer
    this.optionsMenu.addMenuItem(new ajax.components.MenuItem(this.getDocument(), "-"));
    
    // Item 2
    var optionsLoadObjectViewer = new ajax.components.MenuItem(this.getDocument(), "Object Viewer...");
    this.optionsMenu.addMenuItem(optionsLoadObjectViewer);
    optionsLoadObjectViewer.getEvent(this.getWindow(), "onSelected").addListener(function(e)
        {            
            var objectTreeViewer = $new("ajax.windows.FrmJsObjectTreeViewer", this.getDocument(), "");
            objectTreeViewer = ajax.windows.FrmJsObjectTreeViewer.cast(objectTreeViewer);            
            setTimeout(objectTreeViewer.showCentered.bind(objectTreeViewer), 10); // give the mouseup time to fire (which would otherwise bring us back to front).
        }.bind(this)
    );

    // Create MenuBar Items                            
    var fileItem    = ajax.lang.safeNew("ajax.components.MenuItem", this.getDocument(), "File", "", "", null, itemFlags.Text); 
    fileItem        = ajax.components.MenuItem.cast(fileItem);                                       
    var optionsItem = ajax.lang.safeNew("ajax.components.MenuItem", this.getDocument(), "Options", "", "", null, itemFlags.Text); 
    optionsItem     = ajax.components.MenuItem.cast(optionsItem);
    
    // Add Bar Items
    this.getMenuBar().addMenuItem(fileItem, 40);
    this.getMenuBar().addMenuItem(optionsItem, 50);
    
    
    // React to Bar Item Events
    fileItem.getEvent(this.getWindow(), "onSelected").addListener(function(e)
        {
            this.fileMenu.show(null, null, fileItem.element); 
        }.bind(this)
    );
    optionsItem.getEvent(this.getWindow(), "onSelected").addListener(function(e)
        {
            this.optionsMenu.show(null, null, optionsItem.element);
        }.bind(this)
    );
    
}

//-----------------------------------------------------------------------------
ajax.windows.Console.prototype.onLoadHandler = function(e)
{    
    // Make black etc..
    this.copyProperties(this.content.style, this.itemStyle);
    
    // Create Menu    
    this.createMenu();
}

//-----------------------------------------------------------------------------
// Public methods
ajax.windows.Console.prototype.writeLn = function(sText)
{
    sText = (typeof(sText) == "undefined") ? "<br/>" : sText;
    
    // Store the addition to the log as plain HTML
    this.log += sText;
    
    // Its possible that the doc & GUI have gone walkies (unloaded), so we must check for that...
    try
    {
        var div = this.doc.createElement("div");
        div.innerHTML = sText;
        this.content.appendChild(div);       
    }
    catch(ex)
    {
        // No GUI so we are only acting as a holder for the log.
        // If this console window was being used as the debug.Output, set it back to the default
        // and copy the existing log across to it, we can then die...
        
        if (ajax.debug.Output == this)
        {
            // Store current log
            var log = this.log;
        
            // reset debug output to default
            ajax.debug.Output = ajax.debug.defaultOutput;
            //
            ajax.debug.Output.log = log;        
        }
    }
    try
    {
        this.content.doScroll("scrollbarPageDown");
    }
    catch(ex){}       
};

//-----------------------------------------------------------------------------
ajax.windows.Console.prototype.clear = function()
{    
    this.log = "";
    // The GUI may be invalid
    try
    {
        while (this.content.childNodes.length > 0)
        {        
            this.content.childNodes[0].removeNode(true);
        }
    }
    catch(ex)
    {}     
};

//-----------------------------------------------------------------------------
ajax.windows.Console.prototype.toString = function()
{
    return "ajax.windows.Console";    
}

//-----------------------------------------------------------------------------
ajax.windows.Console.prototype.toClassName = function()
{
    return ajax.lang.getClassName("ajax.windows.Console");
}

//-----------------------------------------------------------------------------
ajax.windows.Console.cast = function(obj)
{///<returns type="ajax.windows.Console"/>
    return obj;
}